package com.doradance.app.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import com.doradance.R;
import com.doradance.account.ui.LoginActivity;
import com.doradance.account.utils.ShareUtil;
import com.doradance.entity.FileUtils;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.io.SharePreferenceUtil;

import java.io.File;

import butterknife.Bind;
import butterknife.OnClick;

import static com.doradance.account.AccountManager.getTokenInfo;

public class SetActivity extends BaseActivity {

    @Bind(R.id.tv_cacher)
    TextView mTvCacher;
    @Bind(R.id.cb_3g)
    CheckBox mCb3g;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initToolsBar();
        setCacheSize();
        initIs3g();
    }

    private void initIs3g() {
        if(ShareUtil.getIs3G()==true){
            mCb3g.setChecked(true);
            SharePreferenceUtil.saveBoolean(getApplicationContext(),"is3gCheck","is3gCheck",true);
        }else {
            mCb3g.setChecked(false);
            SharePreferenceUtil.saveBoolean(getApplicationContext(),"is3gCheck","is3gCheck",false);
        }
        mCb3g.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked==true){
                    mCb3g.setChecked(true);
                    ShareUtil.setIs3G(true);
                }else{
                    mCb3g.setChecked(false);
                    ShareUtil.setIs3G(false);
                }
            }
        });
    }

    private void setCacheSize() {
        File cacheDir = getCacheDir();
        String cacheSize = FileUtils.size(cacheDir);
        mTvCacher.setText(cacheSize);
    }

    private void initToolsBar() {
        setTitle(getString(R.string.set));
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_set, null);
    }

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, SetActivity.class);
        activity.startActivity(intent);
    }

    @OnClick({R.id.ll_clean_caching, R.id.ll_about_our, R.id.btn_login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_clean_caching:
                //清除缓存
                final File cacheDir = getCacheDir();
                final String cacheSize = FileUtils.size(cacheDir);
                //弹出Dialog
                showCacheDialog(cacheDir, cacheSize);
                break;
            case R.id.ll_about_our:
                AboutActivity.start(this);
                break;
            case R.id.btn_login_out:
                //退出登录
                final AlertDialogFragment singOutDialogFrag = new AlertDialogFragment();
                singOutDialogFrag.setMessage("是否确定要退出登录?")
                        .setNegativeButton("取消", true, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                singOutDialogFrag.dismiss();
                            }
                        })
                        .setPositiveButton("确定", false, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //删除TokenInfo
                                getTokenInfo().clear();
                                singOutDialogFrag.dismiss();
                                SharePreferenceUtil.saveString(getApplicationContext(), "isOptimize", "isOptimize", "");
                                ShareUtil.setBabyId("");
                                LoginActivity.start(SetActivity.this);
                            }
                        }).show(getSupportFragmentManager());

                break;
        }
    }

    //清理缓存
    private void showCacheDialog(final File cacheDir, final String cacheSize) {
        if ("0B".equals(cacheSize)) {
            final AlertDialogFragment nullCacheDialogFrag = new AlertDialogFragment();
            nullCacheDialogFrag.setMessage("无缓存")
                    .setPositiveButton("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            nullCacheDialogFrag.dismiss();
                        }
                    }).show(getSupportFragmentManager());
        } else {
            final AlertDialogFragment cleanDialogFrag = new AlertDialogFragment();
            cleanDialogFrag.setMessage("确认清除" + cacheSize + "缓存?")
                    .setNegativeButton("取消", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            cleanDialogFrag.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            FileUtils.delFilesFromPath(cacheDir);
                            mTvCacher.setText("0B");
                            //删除消息列表数据库
                            cleanDialogFrag.dismiss();
                            Toast.makeText(getBaseContext(), "已清除" + cacheSize + "缓存!", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .show(getSupportFragmentManager());
        }
    }
}
